package com.sdd.rpi_mobile.infoTypes;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sdd.rpi_mobile.R;
import com.sdd.rpi_mobile.homeScreen.GridChooser;

/** Show information about a building */
public class BuildingInfoActivity extends Activity {

	private TextView mTitle, mDescription;
	private String mPicName, mAddr;
	
	private AlertDialog.Builder mAlert;
	private ImageView mPicture;
	private int mImageResource;
	private Drawable mPicRes;

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.building_info_template);

		// Enable the Home button in the action bar.
		if (Build.VERSION.SDK_INT >= 14){
			getActionBar().setHomeButtonEnabled(true);
		}

		Bundle bundle = this.getIntent().getExtras();
		int buildingId = bundle.getInt("resource");

		Resources res = getResources();
		String[] info = res.getStringArray(buildingId);

		BuildingInfo infoObj = new BuildingInfo(info);

		// Get title.
		mTitle = (TextView) findViewById(R.id.header);
		mTitle.setText(infoObj.getName());

		// Get description.
		mDescription = (TextView) findViewById(R.id.building_description);
		mDescription.setText(infoObj.getDescription());

		setImage(infoObj);

		// Setup the buttons.
		Button navTo = (Button) findViewById(R.id.navigate_button);
		Button where = (Button) findViewById(R.id.where_am_i);
		mAddr = infoObj.getLocation();
		
		boolean mapsInstalled = appInstalledOrNot("com.google.android.apps.maps");

		if (mapsInstalled) {
			// Add a OnClickListener to the navigate to button.
			navTo.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					String uri = "geo:0,0?q=" + mAddr;
					Intent intent = new Intent(
							android.content.Intent.ACTION_VIEW, Uri.parse(uri));
					intent.setClassName("com.google.android.apps.maps",
							"com.google.android.maps.MapsActivity");
					startActivity(intent);
				}
			});
			where.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					String uri = "geo:0,0";
					Intent intent = new Intent(
							android.content.Intent.ACTION_VIEW, Uri.parse(uri));
					intent.setClassName("com.google.android.apps.maps",
							"com.google.android.maps.MapsActivity");
					startActivity(intent);
				}
			});
		} else {
			navTo.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					// Setting up alert dialog and adding "Ok" button.
					mAlert = new AlertDialog.Builder(view.getContext());
					mAlert.setTitle("You need Google Maps App to use this feature!");
					mAlert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							//This button doesn't have any functionally.
							return;
						}
					});
					mAlert.show();
				}
			});
			where.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					// Setting up alert dialog and adding "Ok" button.
					mAlert = new AlertDialog.Builder(view.getContext());
					mAlert.setTitle("You need Google Maps App to use this feature!");
					mAlert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							//This button doesn't have any functionally.
							return;
						}
					});
					mAlert.show();
				}
			});
		}
	}

	private void setImage(BuildingInfo infoObj)
	{
		//Get ImageView and set picture
		mPicture = (ImageView) findViewById(R.id.building_picture);
		mPicName = infoObj.getPicStr();
		mImageResource = getResources().getIdentifier(mPicName, "drawable",
				getPackageName());
		
		if (mImageResource != 0) {
			mPicRes = getResources().getDrawable(mImageResource);
			mPicture.setImageDrawable(mPicRes);
		} else {
			//If no picture is found, set default picture.
			mPicture.setImageResource(R.drawable.building_default);
		}	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.places_info_template, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// App icon in action bar clicked; go home
			Intent intent = new Intent(this, GridChooser.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Method that test if a given application is installed on the phone.
	 * @param uri
	 * URI of application
	 * @return
	 * Wheter the application is installed
	 */
	private boolean appInstalledOrNot(String uri) {
		PackageManager pm = getPackageManager();
		boolean app_installed = false;
		try {
			pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
			app_installed = true;
		} catch (PackageManager.NameNotFoundException e) {
			app_installed = false;
		}
		return app_installed;
	}
}
